package com.sumainfor.pc.gmio.controller.MktFeign;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.gmio.service.impl.market.BillOwnerService;
import com.sumainfor.pc.gmio.service.impl.tenant.BillService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 功能描述:
 * 账单
 *
 * @Auther: zhlu
 * @Date: 2019/3/25 17:48
 * @Description:
 */
@RestController
@RequestMapping("/key/bill")
public class BillOwnerController {

    @Autowired
    BillOwnerService BillOwnerService;

    @Autowired
    BillService BillService;

    /**
     * 功能描述:
     * 根据业主合同资料生成唐巢支付业主的账单
     * 1.新业主收房信息补全之前生成。
     * 2.续签业主在第一份合同到期之后定时任务生成
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/25 10:42
     */
    @ResponseBody
    @PostMapping("/AddBillOwner")
    public JsonResult AddBillOwner(@RequestBody Map<String, Object> params) throws Exception {
        JsonResult result = new JsonResult();
        if (BillOwnerService.AddBillOwner(params)) {
            result.putSuccess("业主账单录入成功！");
        } else {
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 根据业主账单编号获取业主账单资料
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/1 19:32
     */
    @GetMapping("/getOwnerBillMap")
    public JsonResult getOwnerBillMap(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BiOutId"})) {
            result.putFailed("BiOutId不能为空！");
            return result;
        }
        result.put(BillOwnerService.getOwnerBillMap(params));
        return result;
    }

    /**
     * 功能描述:
     * 根据账单编号获取账单基本资料
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/28 13:37
     */
    @GetMapping("/getTrenantBill")
    public JsonResult getTrenantBill(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BiId"})) {
            result.putFailed("BiId不能为空！");
            return result;
        }
        return result.put(BillService.getTrenantBill(params));
    }


    ///////////////////////////////////////////////////////////////////////////
    // 企业账单创建
    ///////////////////////////////////////////////////////////////////////////


    /**
     * 创建企业账单临时账单
     *
     * @param params
     * @return
     */
    @PostMapping("/createTempOrder")
    public JsonResult createTempOrder(@RequestBody Map<String, Object> params) {
        if (MapUtils.isEmpty(params)) {
            return JsonResult.errorForEmpty();
        }
        return BillOwnerService.createTempOrder(params);
    }

    /**
     * 创建主账单
     *
     * @param params
     * @return
     */
    @PostMapping("/createOrderMain")
    public JsonResult createOrderMain(@RequestBody Map<String, Object> params) {
        if (MapUtils.isEmpty(params)) {
            return JsonResult.errorForEmpty();
        }
        return BillOwnerService.createOrderMain(params);
    }

    /**
     * 查询退租账单
     *
     * @param witId
     * @return
     */
    @PostMapping("/getBackOrder")
    public JsonResult getBackOrder(String witId) {
        if (StringUtils.isBlank(witId)) {
            return JsonResult.errorForEmpty();
        }
        return BillOwnerService.getBackOrder(witId);
    }
}
